﻿/**
 *    Copyright 2012 Yohan Courbe
 *
 *  Licensed under the Apache License, Version 2.0 (the "License 
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *      
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */


using System;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using MySql.Data.MySqlClient;

namespace NetworkAnalyzer
{
    static class Tools
    {
        public static bool IsLocalHost(this IPAddress host)
        {
            return (host.ToInt() > NetworkAnalyzer.IpNetwork.FirstUsable.ToInt() &&
                    host.ToInt() < NetworkAnalyzer.IpNetwork.LastUsable.ToInt());
        }

        public static string GetFormatedMacAddress(PhysicalAddress ph)
        {
            return BitConverter.ToString(ph.GetAddressBytes());
        }

        private static double DegreeToRadian(double angle)
        {
            return Math.PI * angle / 180.0;
        }

        public static PointF PointFromPolarCoordinate(double r, double theta)
        {
            var x = (int)(r * Math.Cos(DegreeToRadian(theta)));
            var y = (int)(r * Math.Sin(DegreeToRadian(theta)));

            return new System.Drawing.PointF(x, y);
        }

        public static void CartesianToPolar(float x, float y, out double angle, out double radius)
        {
            radius = Math.Sqrt(x*x + y*y);
            angle = Math.Atan(x/y);
        }

        public static string GetFormattedBandwidth(int bytes)
        {
            int megabytes = bytes / 1048576;
            int rest = bytes % 1048576;

            int kilobytes = rest / 1024;
            rest = rest % 1024;

            bytes = rest;

            return String.Format("{0}MB {1}KB {2}B", megabytes, kilobytes, bytes);
        }

        public static string GetMySqlFormattedDate(this DateTime date)
        {
            //string mysqlDate = String.Format("{0}-{1}-{2} {3}:{4}:{5}.{6}", date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, date.Millisecond);
            //Console.WriteLine(mysqlDate);
            return date.ToString("yyyy-MM-dd HH:mm:ss");
        }

        public static int GetMysqlBoolean(this bool boolean)
        {
            return Convert.ToInt32(boolean);
        }

        public static string MySqlEscape(this string usString)
        {
            return MySqlHelper.EscapeString(usString);
        }

        public static ApplicativeProtocol GetApplicativeProtocol(ushort port)
        {
            switch (port)
            {
                case 22:
                    return ApplicativeProtocol.Ssh;
                case 21:
                case 20:
                    return ApplicativeProtocol.Ftp;
                case 23:
                    return ApplicativeProtocol.Telnet;
                case 25:
                    return ApplicativeProtocol.Smtp;
                case 43:
                    return ApplicativeProtocol.Whois;
                case 53:
                    return ApplicativeProtocol.Dns;
                case 80:
                    return ApplicativeProtocol.Http;
                case 110:
                    return ApplicativeProtocol.Pop3;
                case 123:
                    return ApplicativeProtocol.Ntp;
                case 143:
                    return ApplicativeProtocol.Imap;
                case 161:
                    return ApplicativeProtocol.Snmp;
                case 443:
                    return ApplicativeProtocol.Https;
                case 843:
                    return ApplicativeProtocol.AdobeFlash;
                case 993:
                    return ApplicativeProtocol.Imaps;
                case 995:
                    return ApplicativeProtocol.Pop3s;
                case 3306:
                    return ApplicativeProtocol.Mysql;
                case 3389:
                    return ApplicativeProtocol.RemoteDesktop;
                case 1863:
                case 5190:
                    return ApplicativeProtocol.MsnMessenger;
                case 5938:
                    return ApplicativeProtocol.TeamViewer;
                case 33033:
                    return ApplicativeProtocol.Skype;
            }

            if (port >= 6891 && port <= 6901)
            {
                return ApplicativeProtocol.MsnMessenger;
            }

            return ApplicativeProtocol.Unknown;
        }

        public static ApplicativeProtocol GetApplicativeProtocolByPort(ushort sourceport, ushort destport)
        {
            var applicativeProtocol = GetApplicativeProtocol(sourceport);

            if (applicativeProtocol == ApplicativeProtocol.Unknown)
            {
                applicativeProtocol = GetApplicativeProtocol(destport);
            }

            return applicativeProtocol;
        }

        public static uint ToInt(this IPAddress ip)
        {
            return BitConverter.ToUInt32(ip.GetAddressBytes().Reverse().ToArray(), 0);
        }
    }
}
